package com.doris.media.picker.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.ActivityInfo
import android.content.res.TypedArray
import android.os.Build
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.app.ActivityCompat
import com.bumptech.glide.Glide
import com.doris.media.picker.R
import com.doris.media.picker.model.MediaPickerConfig
import com.doris.media.picker.model.MediaPickerPreviewParameter
import com.qmuiteam.qmui.arch.QMUIActivity
import com.qmuiteam.qmui.arch.SwipeBackLayout
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import kotlinx.android.synthetic.main.media_picker_preview_image_activity.*
import org.jetbrains.anko.contentView

@SuppressLint("all")
class PreviewImageActivity : QMUIActivity() {

    private lateinit var mParameter: MediaPickerPreviewParameter

    override fun onCreate(savedInstanceState: Bundle?) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            fixOrientation()
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.media_picker_preview_image_activity)
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        mParameter = intent.getParcelableExtra(MediaPickerConfig.MEDIA_PICKER_PREVIEW)
            ?: MediaPickerPreviewParameter()
        // 右滑返回取消
        if (contentView is SwipeBackLayout) {
            (contentView as SwipeBackLayout).isEnableSwipeBack = false
        }
        // 状态栏
        try {
            if (mParameter.statusTheme == MediaPickerConfig.STATUS_THEME_DARK) {
                QMUIStatusBarHelper.setStatusBarDarkMode(this)
            } else {
                QMUIStatusBarHelper.setStatusBarLightMode(this)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        // 图片
        Glide.with(this).load(mParameter.path).into(mediaPicker_photo_view)
        mediaPicker_photo_view.setOnClickListener {
            ActivityCompat.finishAfterTransition(this)
        }
    }

    /**
     * 8.0.0适配
     */
    private fun fixOrientation(): Boolean {
        try {
            val field = Activity::class.java.getDeclaredField("mActivityInfo")
            field.isAccessible = true
            val o = field[this] as ActivityInfo
            o.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
            field.isAccessible = false
            return true
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    private fun isTranslucentOrFloating(): Boolean {
        var isTranslucentOrFloating = false
        try {
            val styleableRes = Class.forName("com.android.internal.R\$styleable")
                .getField("Window")[null] as IntArray
            val ta = obtainStyledAttributes(styleableRes)
            val m = ActivityInfo::class.java.getMethod(
                "isTranslucentOrFloating", TypedArray::class.java
            )
            m.isAccessible = true
            isTranslucentOrFloating = m.invoke(null, ta) as Boolean
            m.isAccessible = false
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return isTranslucentOrFloating
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return
        }
        super.setRequestedOrientation(requestedOrientation)
    }
}